package com.example.services;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;

public class MyService extends Service {
    int counter = 0;
    URL[] urls;
    static final int UPDATE_INTERVAL = 1000;
    private Timer timer = new Timer();
    private DynamicTimerTask timerTask;
    private final IBinder binder = new MyBinder();

    public class MyBinder extends Binder{
        MyService getService(){
            return MyService.this;
        }
    }

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        Log.d("MyService", " Service Started");

        timerTask = new DynamicTimerTask();

        timer.scheduleAtFixedRate(timerTask, 0, UPDATE_INTERVAL);

//            int result = downloadFile(new URL("http://www.ynnu.edu.cn"));
//
//            Toast.makeText(getBaseContext(), "Downloaded " + result + "bytes", Toast.LENGTH_LONG).show();

//            new DoBackgroundTask().execute(new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"),
//                    new URL("http://www.ynnu.edu.cn"));
        new DoBackgroundTask().execute(urls);

        return  START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();


        if(timer != null){
            timer.cancel();
        }
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
        Log.d("MyService", " Service Destroyed");
    }

    private int downloadFile(URL url){

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e){
            e.printStackTrace();
        }

        return 100;
    }

    private class DoBackgroundTask extends AsyncTask<URL, Integer, Long>{

        @Override
        protected Long doInBackground(URL... urls) {
            int count = urls.length;
            long totalBytesDownloaded = 0;
            for(int i = 0; i < urls.length; i++){
                totalBytesDownloaded += downloadFile(urls[i]);

                publishProgress((int) (((i + 1) /(float) count) * 100));
            }
            return totalBytesDownloaded;
        }

        @Override
        protected void onPostExecute(Long aLong) {

            Toast.makeText(getBaseContext(), "Downloaded " + aLong + "bytes", Toast.LENGTH_LONG).show();

            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("FILE_DOWNLOADED_ACTION");
            getBaseContext().sendBroadcast(broadcastIntent);

            stopSelf();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            Log.d("Downloading file", String.valueOf(values[0]) + "% downloaded");

            Toast.makeText(getBaseContext(), String.valueOf(values[0]) + "% downloaded", Toast.LENGTH_LONG).show();

            int interval = values[0]/10 + 1;

            Log.d("UPDATE_INTERVAL", String.valueOf(interval) + " Seconds");

            timerTask.setPeriod(interval * 1000);

        }
    }
}
